package com.pscsw.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.pscsw.MyBatisUtil;
import com.pscsw.dao.UserMapper;
import com.pscsw.po.User;

/*
UserMapper:
User selectByAccount(User record);
<select id="selectByAccount"
		parameterType="com.pscsw.po.User"
		resultMap="BaseResultMap">
		<!-- WARNING - @mbg.generated This element is automatically generated by 
			MyBatis Generator, do not modify. This element was generated on Tue Apr 17 
			08:04:15 CST 2018. -->
		select
		<include refid="Base_Column_List" />
		from user
		where account = #{account,jdbcType=VARCHAR}
	</select>
*/

/**
 * Servlet implementation class loginServlet
 */
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public loginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		// 登入
		String account = request.getParameter("account");
		String password = request.getParameter("password");
		System.out.println(account);
		if (account != null && password != null && !account.isEmpty() && !password.isEmpty()) {
			User user = new User();
			user.setAccount(request.getParameter("account"));

			SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession();
			if (session != null) {
				UserMapper userMsg = session.getMapper(UserMapper.class);
				user = userMsg.selectByAccount(user);
				session.commit();
				session.close();
			}

			if (user != null && user.getPassword().equals(password)) {// 賬號密碼正確
				request.getSession().setAttribute("user", user);
				request.getSession().setAttribute("account", user.getAccount());
				request.getSession().setAttribute("name", user.getName());
				response.getWriter().append("ok");
			} else {
				request.getSession().setAttribute("account", "");
				request.getSession().setAttribute("name", "");
				response.getWriter().append("登錄失败,请重试！");
			}
		} else {// 登錄失败
			request.getSession().setAttribute("account", "");
			request.getSession().setAttribute("name", "");
			response.getWriter().append("登錄失败,请重试！");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
